home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / bsddb / dbutils.pyc (.txt) < prev   
Python Compiled Bytecode  |  2009-11-11  |  2KB  |  58 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from time import sleep as _sleep
  5. import sys
  6. absolute_import = sys.version_info[0] >= 3
  7. if absolute_import:
  8.     exec 'from . import db'
  9. else:
  10.     import db
  11. _deadlock_MinSleepTime = 1 / 128
  12. _deadlock_MaxSleepTime = 3.14159
  13. _deadlock_VerboseFile = None
  14.  
  15. def DeadlockWrap(function, *_args, **_kwargs):
  16.     '''DeadlockWrap(function, *_args, **_kwargs) - automatically retries
  17.     function in case of a database deadlock.
  18.  
  19.     This is a function intended to be used to wrap database calls such
  20.     that they perform retrys with exponentially backing off sleeps in
  21.     between when a DBLockDeadlockError exception is raised.
  22.  
  23.     A \'max_retries\' parameter may optionally be passed to prevent it
  24.     from retrying forever (in which case the exception will be reraised).
  25.  
  26.         d = DB(...)
  27.         d.open(...)
  28.         DeadlockWrap(d.put, "foo", data="bar")  # set key "foo" to "bar"
  29.     '''
  30.     sleeptime = _deadlock_MinSleepTime
  31.     max_retries = _kwargs.get('max_retries', -1)
  32.     if _kwargs.has_key('max_retries'):
  33.         del _kwargs['max_retries']
  34.     
  35.     while True:
  36.         
  37.         try:
  38.             return function(*_args, **_kwargs)
  39.         continue
  40.         except db.DBLockDeadlockError:
  41.             if _deadlock_VerboseFile:
  42.                 _deadlock_VerboseFile.write('dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
  43.             
  44.             _sleep(sleeptime)
  45.             sleeptime *= 2
  46.             if sleeptime > _deadlock_MaxSleepTime:
  47.                 sleeptime = _deadlock_MaxSleepTime
  48.             
  49.             max_retries -= 1
  50.             if max_retries == -1:
  51.                 raise 
  52.             max_retries == -1
  53.             continue
  54.         
  55.  
  56.         None<EXCEPTION MATCH>db.DBLockDeadlockError
  57.  
  58.